#include "Fight.h"
#include <iostream>
#include <stdlib.h>
#include<easyx.h>
//#include <time.h>

using namespace std;

//别踩白块儿模块
#define TITLE 100
#define GRID_W 100
#define GRID_H 150
int flag[4] = { 3,0,1,2 };//存储每一行黑块的编号

bool isOver = false;//游戏结束的标志

int score = 0;//分数

int startTime = 0;//计时

//初始化
void init() {
	for (int i = 0; i < 4; i++) {
		flag[i] = rand() % 4;
	}
}

//游戏绘制
void draw() {
	settextcolor(BLACK);
	settextstyle(20, 0, "黑体");
	char tt[100] = "别踩白块儿";
	sprintf(tt, "别踩白块儿 分数: %d 时长:%d", score, startTime ? (clock() - startTime) / 1000 : 0);

	int space = (getwidth() - textwidth(tt)) / 2;//字符串宽度获取

	outtextxy(space, 20, tt);//标题居中显示

	setlinecolor(BLACK);//绘制线条
	setlinestyle(PS_SOLID, 2);
	//绘制格子
	for (int i = 0; i < 5; i++) {
		line(0, TITLE + i * GRID_H, 400, TITLE + i * GRID_H);
		line(i * GRID_W, 100, i * GRID_W, 700);
	}
	//绘制黑方块
	setfillcolor(BLACK);
	for (int i = 0; i < 4; i++) {
		int x = flag[i] * GRID_W;
		int y = i * GRID_H + TITLE;//待定

		solidrectangle(x, y, x + GRID_W, y + GRID_H);
	}
}

//处理鼠标点击
void mouseMsg(ExMessage* msg) {
	if (startTime == 0) {
		startTime = clock();
	}
	int x = flag[3] * GRID_W;
	int y = 3 * GRID_H + TITLE;//
	if (msg->x >= x && msg->x <= x + GRID_W && msg->y >= y && msg->y <= y + GRID_H && ((clock() - startTime) *0.001) <= 10) {
		for (int i = 3; i > 0; i--) {
			flag[i] = flag[i - 1];
		}
		flag[0] = rand() % 4;
		score++;
	}
	else {
		isOver = true;
	}
}
//上面是别踩白块儿模块

Fight::Fight(Role player, Enemy* enemy) :player(player), enemy(*enemy) { round = 1; };
//Fight::Fight(Role player, Enemy_npc enemy_npc) : player(player), enemy(enemy_npc) { round = 1; };
//Fight::Fight(Role player, Enemy_Boss enemy_Boss) : player(player), enemy(enemy_Boss) { round = 1; };

Fight::~Fight() {};

void Fight::ShowFight() {   //展示战斗时状态
	player.showRole();
	cout << endl;
	enemy.displayInfo();
	cout << endl << "当前为第" << round << "回合" << endl;
}

void Fight::FightChoice() {         //角色战斗选择
	int choose = 0;
	cout << "请选择: 1.普通攻击 2.使用秘籍 3.使用药品 4.逃跑" << endl;
	//cin >> choose;
	while (true) {
		if (cin >> choose) {
			// 输入有效
			break; // 退出循环
		}
		else {
			// 输入无效
			cin.clear(); // 清除错误状态
			cin.ignore(1000, '\n'); // 忽略错误输入
			cout << "无效输入，请重新输入。" << std::endl;
		}
	}
	if (choose >= 1 && choose <= 4) {
		switch (choose) {
		case 1:
		{
			if ((enemy.getArmor() - player.getAttack()) > 0) {		//敌人护甲值大于角色本次输出
				enemy.setArmor(enemy.getArmor() - player.getAttack());
				cout << "敌人的护甲抵挡了你的攻击！" << endl;
				break;
			}
			if ((enemy.getArmor() - player.getAttack()) == 0) {			//敌人护甲值等于角色本次输出
				enemy.setArmor(0);
				cout << "敌人的护甲被你摧毁了！" << endl;
				break;
			}
			if (((enemy.getArmor() - player.getAttack()) < 0) && enemy.getArmor() > 0) {			//敌人护甲值小于角色本次输出但原来大于0
				enemy.setArmor(0);
				if (enemy.getHealth() > (player.getAttack() - enemy.getArmor())) {			//敌人生命值大于角色摧毁护甲溢出的伤害
					enemy.setHealth(enemy.getHealth() - player.getAttack() + enemy.getArmor());
					cout << "你摧毁了敌人的护甲并且造成了" << player.getAttack() - enemy.getArmor() << "点伤害！" << endl;
					break;
				}
				else {			//敌人生命值小于角色摧毁护甲溢出的伤害
					enemy.setHealth(0);
					cout << "你击杀了敌人！" << endl;
					break;
				}
			}
			else {			//敌人护甲值在角色攻击时已经为0
				if (enemy.getHealth() > player.getAttack()) {
					enemy.setHealth(enemy.getHealth() - player.getAttack());
					cout << "你对敌人造成了" << player.getAttack() << "点伤害" << endl;
					break;
				}
				else {			//敌人生命值小于角色本次输出
					enemy.setHealth(0);
					cout << "你击杀了敌人！" << endl;
					break;
				}
			}
			break;
		}
		case 2:
		{
			if (player.showSkill()) {
				double SkillAttack = player.useSkill();
				if ((enemy.getArmor() - SkillAttack) > 0) {		//敌人护甲值大于角色本次输出
					enemy.setArmor(enemy.getArmor() - SkillAttack);
					cout << "敌人的护甲抵挡了你的攻击！" << endl;
					break;
				}
				if ((enemy.getArmor() - SkillAttack) == 0) {			//敌人护甲值等于角色本次输出
					enemy.setArmor(0);
					cout << "敌人的护甲被你摧毁了！" << endl;
					break;
				}
				if (((enemy.getArmor() - SkillAttack) < 0) && enemy.getArmor() > 0) {			//敌人护甲值小于角色本次输出但原来大于0
					enemy.setArmor(0);
					if (enemy.getHealth() > (SkillAttack - enemy.getArmor())) {			//敌人生命值大于角色摧毁护甲溢出的伤害
						enemy.setHealth(enemy.getHealth() - SkillAttack + enemy.getArmor());
						cout << "你摧毁了敌人的护甲并且造成了" << SkillAttack - enemy.getArmor() << "点伤害！" << endl;
						break;
					}
					else {			//敌人生命值小于角色摧毁护甲溢出的伤害
						enemy.setHealth(0);
						cout << "你击杀了敌人！" << endl;
						break;
					}
				}
				else {			//敌人护甲值在角色攻击时已经为0
					if (enemy.getHealth() > SkillAttack) {
						enemy.setHealth(enemy.getHealth() - SkillAttack);
						cout << "你对敌人造成了" << SkillAttack << "点伤害" << endl;
						break;
					}
					else {			//敌人生命值小于角色本次输出
						enemy.setHealth(0);
						cout << "你击杀了敌人！" << endl;
						break;
					}
				}
			}
			break;
		}
		case 3:
		{
			//player.showDrug();
			player.useDrug();
			break;
		}
		case 4:
		{
			srand((unsigned)time(NULL));
			if (rand() % 100 <= 50) {			//50%成功
				cout << "逃跑失败!" << endl;
				break;
			}
			else {
				cout << "逃跑成功!" << endl;
				round = -1;	//作为战斗结束的标准
				return;		//直接跳出函数
			}
			break;
		}
		}
		round++;
	}
	else cout << "无效输入，请重新选择: " << endl;
}

int Fight::EnemyFight() {        //敌人战斗选择
	int enemyId = enemy.getId();
	int enemyAttack = 0, hurt = 0;
	srand((unsigned)time(NULL));
	if (enemyId > 0 || enemyId < 5) enemyAttack = enemy.getAttack();
	if (enemyId > 4 || enemyId < 8) {
		if (rand() % 100 <= 50) enemyAttack = enemy.getAttack();
		else {
			Enemy_npc* EnemyNPC = dynamic_cast<Enemy_npc*>(&enemy);//类型转换，因为enemy类中不含getSkill函数
			//这里是比较npc剩余内力和秘籍所需内力
			//由于这几个npc和skill的id都是线性相关，所以简单处理
			if (EnemyNPC != nullptr) {
				if (EnemyNPC->getInternalEnergy() > (EnemyNPC->getSkill()).getNeedMp(enemyId + 16)) {
					enemyAttack = (EnemyNPC->getSkill()).getAddAttack(enemyId + 16);
					EnemyNPC->setInternalEnergy(EnemyNPC->getInternalEnergy() - (EnemyNPC->getSkill()).getNeedMp(enemyId + 16));//减少内力
				}
				else {
					cout << "敌人的内力值不足，无法发动秘籍，没有对你造成伤害。" << endl;
					return 0;
				}
			}

		}
	}
	if (enemyId == 8) {
		Enemy_Boss* EnemyBOSS = dynamic_cast<Enemy_Boss*>(&enemy);
		int sr = rand() % 100;
		if (sr <= 25) enemyAttack = enemy.getAttack();
		if (sr > 25 && sr < 65) enemyAttack = (EnemyBOSS->getSkill()).getAddAttack(24);
		if (sr >= 65 && sr <= 90) enemyAttack = (EnemyBOSS->getSkill()).getAddAttack(25);
		if (sr > 90 && sr <= 100) enemyAttack = (EnemyBOSS->getSkill()).getAddAttack(26);
	}
	hurt = enemyAttack - player.getArmor();
	if (player.getArmor() == 0) {
		player.setHealth(player.getHealth() - enemyAttack);
		cout << "敌人对你造成了" << enemyAttack << "点的伤害" << endl;
	}
	else if (hurt < 0) {
		player.setDefend(player.getDefend() - enemyAttack);
		cout << "你的防御力抵挡了敌人的普通攻击，没有对你造成伤害。" << endl;
	}
	else {
		player.setDefend(0);
		cout << "敌人摧毁了你的护甲";
		if (hurt > 0) {
			player.setHealth(player.getHealth() - enemyAttack);
			cout << "并对你造成了" << enemyAttack << "点的伤害" << endl;
		}
		cout << endl;
	}

}

int Fight::FightJudgement() {     //判断战斗是否结束
	if (player.getHealth() * enemy.getHealth() == 0) return 0;
	else return 1;
}

int Fight::GetMoney() {     //获得金钱
	cout << "获得了" << enemy.getMoney() << "金钱" << endl;
	player.setMoney(player.getMoney() + enemy.getMoney());
	return player.getMoney() + enemy.getMoney();
}

void Fight::GetSkill() {
	int enemyId = enemy.getId();
	if (enemyId > 5)return;
	if (enemyId == 4) {
		player.addGoodsToBag(24, 1);
		player.addGoodsToBag(25, 1);
		player.addGoodsToBag(26, 1);
		player.AddSkill(24);
		player.AddSkill(25);
		player.AddSkill(26);
	}
	else {
		if (enemyId == 1) {
			player.addGoodsToBag(21, 1);
			player.AddSkill(21);
		}
		if (enemyId == 2) {
			player.addGoodsToBag(22, 1);
			player.AddSkill(22);
		}
		if (enemyId == 3) {
			player.addGoodsToBag(23, 1);
			player.AddSkill(23);
		}


	}
}

int Fight::GetIntelnalEngery() {         //获得内力
	int enemyId = enemy.getId();
	if (enemyId > 4 || enemyId < 8) {
		player.setMp(player.getMp() + enemy.getInternalEnergy());
		cout << "你的内力上限提高了" << player.getMp() + enemy.getInternalEnergy() << "点！" << endl;
	}
	else return 0;
}

Role Fight::Fighting() {
	if (enemy.getId() != 9) {
		while (FightJudgement()) {
			ShowFight();

			FightChoice();
			EnemyFight();
			if (round == -1)return player;		//逃跑成功结束战斗
			Sleep(1200);
			system("cls");
		}
		if (player.getHealth() > 0 && round != -1) {
			cout << "战斗胜利！" << endl;
			switch (enemy.getId()) {
			case 1:
				EnemyCount[1] = 1;
				break;
			case 2:
				EnemyCount[2] = 1;
				break;
			case 3:
				EnemyCount[3] = 1;
				break;
			case 4:
				EnemyCount[4] = 1;
				break;
			case 9:
				EnemyCount[0] = 1;
				break;
			default:
				break;
			}
			GetIntelnalEngery();
			GetSkill();
			GetMoney();
			return player;
		}
		else {
			if (enemy.getHealth() > 0)cout << "你已死亡，战斗结束" << endl;
			else cout << "很遗憾，你与敌人重归于尽了" << endl;
			cout << "你在江湖中的一段旅程已暂时结束，新的风云正待你来书写。重整旗鼓，重启冒险吧！" << endl;
			//我加的
			player.newBag();//背包清空
			player.NewRole();
			//
			system("pause");
			exit(0);
		}
	}
	else {
		srand(time(NULL));//随机数种子
		initgraph(400, 700);//创建游戏窗口
		//游戏初始化
		setbkcolor(WHITE);
		cleardevice();
		init();
		BeginBatchDraw();  // 开启双缓冲
		ExMessage em;//游戏主循环
		while (1) {
			cleardevice(); //清屏
			draw();//游戏绘制
			//用户输入
			if (peekmessage(&em, EX_MOUSE)) {
				//鼠标左键点击
				if (em.message == WM_LBUTTONDOWN) {
					mouseMsg(&em);
				}
			}
			//判断游戏是否结束
			if (isOver) {//死亡或游戏时长超过10s游戏都会自动结束
				MessageBox(GetHWnd(), "游戏结束", "提示", MB_OK);//MB_OK传递一个按钮
				break;
			}
			//游戏刷新
			FlushBatchDraw();
		}
		//关闭双缓冲
		EndBatchDraw();
		//关闭游戏窗口
		closegraph();
		if (score >= 30) {
			cout << "你：嘿嘿嘿，我赢了，把你的宝物拿来吧。" << endl;
			cout << "修格斯：小兄弟，你是真不不知道我修格斯是怎么安安稳稳活这么久的嘛，哈哈哈" << endl;
			cout << "PS:突然，修格斯偷袭了你" << endl;
			cout << "你...你你...！" << endl;
			cout << "不要气馁，秋少侠，请下次再来。" << endl;
			
			exit(0);
			
			
		}
		else {
			cout<<"修格斯：哈哈哈，小兄弟，还是回去再多练练吧！"<<endl;
			cout << "不要气馁，秋少侠，请下次再来。" << endl;
			player.newBag();//背包清空
			player.NewRole();
			exit(0);
		}
	}

}

bool Fight::IsPlayerLive() {
	if (player.getHealth())return true;
	else return false;
}
